package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.util.*;
import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Retreive list of ALL Users irrespective of Tag. 
 * @author S.N.Fraser
 * $Id: LIST_ALL_USERSImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */
public class LIST_ALL_USERSImpl extends TransactionImpl {

    public LIST_ALL_USERSImpl(LIST_ALL_USERS request) {
	super(request);
    }

    /**
     * Retrieves the List of Users for Tag specified
     * @return list or an OSS_Error.
     */
    public TRANSACTION_DONE exec() { 

	Root root = null;
	Tag  tag  = null;
	User user = null;
	
	Vector list = new Vector(); // Holds the list of Proposal paths.
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	    
	    
	    root = OSS_Utilities.fetchRoot();
	    
	    // Check it exists.
	    if (root == null) {
		logger.log(1, CLASS, id, "exec", "Root: "+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
			     "Could not find Root:");
	    } 
	    
	    // Iterate round the Root's Tags.
	    Iterator it = root.listAllTags();
	    
	    while (it.hasNext()) {
		
		tag = (Tag)it.next();
	    
		// Check it exists.
		if (tag == null) {
		    logger.log(1, CLASS, id, "exec", 
			       "Tag: "+tag+" Not in database.");
		    return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
				 "Could not find Tag: "+tag);
		}
		
		// Iterate round the Tag's Users.
		Iterator iu = tag.listAllUsers();
		
		while (iu.hasNext()) {
		    user = (User)iu.next();
		    list.addElement(user.getDescriptor());
		}
	    }
	} finally {
	    t.commit();
	}
	
	LIST_ALL_USERS_DONE done  = new LIST_ALL_USERS_DONE(id);
	done.setUserList(list);
	done.setSuccessful(true);
	return done;

    }

}

/** $Log: LIST_ALL_USERSImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2006/11/27 08:37:05  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/23 18:51:14  snf
/** Initial revision
/** */
